<?php

namespace App\Http\Controllers\Index;

use App\Http\Requests\Index\SignUp\CreateRequest;
use App\Models\Admin\Recruitment;
use App\Models\Admin\RecruitmentAd;
use App\Models\Admin\RecruitmentSignUp;
use App\Models\Api\CompanyBasic;
use Illuminate\Http\Request;
use Style, DB;

class JobFairController extends CommonController{
    /**
     * 招聘会页面
     * @param $type
     * @return mixed
     */
    public function jobFair(){
        //招聘会背景图片
        $recruitmentAd = RecruitmentAd::first(['pic']);
        $recruitmentPic = (!empty($recruitmentAd) && !empty($recruitmentAd->pic)) ? '/upload/recruitmentad/' . $recruitmentAd->pic : '';
        //招聘会列表
        $newRecruitment = Recruitment::where('hold_time', '>=', date('Y-m-d', time()))->orderBy('hold_time', 'asc')->get();
        if(count($newRecruitment) > 0){
            foreach($newRecruitment as $v){
                //星期几
                $v->week = get_week($v->hold_time);
                //日期
                list($date1, $date2) = explode('-', ltrim(substr($v->hold_time, 5), '0'));
                $v->hold_date = $date1 . '月' . $date2 . '日';
            }
        }
        //检查企业是不是登录
        if(auth()->guard('company')->check()){
            $user = auth()->guard('company')->user();
            $companyBasic = CompanyBasic::where('uid', $user->uid)->first(['company_name', 'contacts', 'mobile', 'phone', 'uid']);
            $mobile = (!empty($companyBasic->mobile)) ? '手机:' . trim($companyBasic->mobile) : '';
            $phone = (!empty($companyBasic->phone)) ? '电话:' . trim($companyBasic->phone) : '';
            $companyBasic->phone = trim($mobile . ' | ' . $phone, ' | ');
        }else{
            $companyBasic = '';
        }

        //往期招聘会列表
        $oldRecruitment = Recruitment::select('job_title', 'hold_time', 'id', 'hold_unit', 'hold_address')
            ->where('hold_time', '<', date('Y-m-d', time()))
            ->orderBy('hold_time', 'desc')
            ->paginate(10);

        return Style::view("index.jobFair", compact('recruitmentPic', 'newRecruitment', 'oldRecruitment', 'companyBasic'));
    }


    /**
     * 招聘报名企业
     * @param CreateRequest $request
     */
    public function signUp(CreateRequest $request){
        $signUp = RecruitmentSignUp::where('recruitment_id', $request->recruitment_id)
            ->where('com_uid', $request->com_uid)->where('com_uid', '!=', '0')->first();
        if(empty($signUp)){
            $input = $request->only('recruitment_id', 'com_uid', 'contact_person', 'phone', 'company');
            $result = RecruitmentSignUp::create($input);
            if($result){
                return response()->json(['status' => 0, 'msg' => '恭喜你,企业报名成功！']);
            }else{
                return response()->json(['status' => 1, 'msg' => '企业报名失败，可能当前招聘会不存在！']);
            }
        }else{
            return response()->json(['status' => 1, 'msg' => '企业报名失败,你已经报名这个招聘会！']);
        }
    }

    /**
     * 招聘会详情页面
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function jobFairInfo($id){
        $recruitment = Recruitment::where('id', $id)->first();
        if(!empty($recruitment)){
            //星期几
            $recruitment->week = get_week($recruitment->hold_time);
            //日期
            list($date1, $date2) = explode('-', ltrim(substr($recruitment->hold_time, 5), '0'));
            $recruitment->hold_date = $date1 . '月' . $date2 . '日';
            //参会企业
            $arr = preg_split('/((\r\n)|(\s+))/Uis',$recruitment->to_attend);
            $recruitment->to_attend = array_values(array_filter($arr));

            //检查企业是不是登录
            if(auth()->guard('company')->check()){
                $user = auth()->guard('company')->user();
                $companyBasic = CompanyBasic::where('uid', $user->uid)->first(['company_name', 'contacts', 'mobile', 'phone', 'uid']);
                $mobile = (!empty($companyBasic->mobile)) ? '手机:' . trim($companyBasic->mobile) : '';
                $phone = (!empty($companyBasic->phone)) ? '电话:' . trim($companyBasic->phone) : '';
                $companyBasic->phone = trim($mobile . ' | ' . $phone, ' | ');
            }else{
                $companyBasic = '';
            }
            return Style::view("index.jobFairInfo", compact('recruitment', 'companyBasic'));
        }else{
            return redirect('404');
        }
    }

    /**
     * 交通线路地图
     * @return mixed
     */
    public function trafficMap(Request $request){
        $recruitment = Recruitment::where('id', $request->id)->first(['job_title', 'longitude', 'latitude', 'hold_address']);
        if(!empty($recruitment)){
            return Style::view("index.trafficMap", compact('recruitment'));
        }
    }
}